﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcJE.Core.Helpers;
using MvcJE.Core.Extensions;
using System.Web.Script.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using MvcJE.Core.Inet;

namespace MvcJE.Web.Areas.Inet.Controllers
{
    public class MopExpertController : Controller
    {
        //
        // GET: /Inet/MopExpert/


        public ActionResult Index()
        {

            var file = AppDomain.CurrentDomain.BaseDirectory + "App_Data\\Templates\\text.txt";
            KeyValueModel k = new KeyValueModel
            {
                Key = "VAR1",
                Value = ""

            };
            KeyBagModel k2 = new KeyBagModel();
            var a = new Dictionary<String, String>();
            a["Key"] = "Value";
            k2.Bag = a;

            JsonFileHelper.SaveJsonStringFile<KeyBagModel>(file, new List<KeyBagModel> { k2, k2 });

            return View();
        }

        [HttpPost]
        public ActionResult Index(List<KeyValueModel> Param)
        {
            var p = Param;
            var c = p;
            return View();
        }

        public ActionResult CreateMOP(int id = 0)
        {
            var s1 = "the quick brown [{Very Long Parameter Name}] fox  [{ Very Long Parameter Name:\"(+1)\"}] [{ Very Long Parameter Name:\"(2)\"}]  [{ Very Long Parameter Name:\"(-2)\"}]  jump [{var 2:\"12345\"}] another [{var3:\"ip route {0} 21.1.1.\"}]";
            var r1 = s1.KeyParserToJSONString();
            var r2 = MOPHelper.ConvertKeyValueToKeyData(r1, true);
            List<KeyDataModel> kdms = new List<KeyDataModel>();

            return View(r2);
        }

        [HttpPost]
        public MvcHtmlString CreateMOP(List<KeyDataModel> param)
        {
            var mop = string.Empty;

            mop = "the quick brown [{Very Long Parameter Name}] fox  [{ Very Long Parameter Name:\"(+1)\"}] [{ Very Long Parameter Name:\"(2)\"}]  [{ Very Long Parameter Name:\"(-2)\"}]  jump [{var 2:\"12345\"}] another [{var3:\"ip route {0} 21.1.1.\"}] \r\n// this is a comment \r\n";
            var keys = mop.KeyParserToJSONString();
            var keyvals = MOPHelper.ConvertKeyValueToKeyData(keys);

            // let's get the input value and assign to original parameters. We have to do this because the input data are distinct paramaters
            foreach (var item in keyvals)
            {
                var varData = param.SingleOrDefault(p => p.UniqueKey == item.UniqueKey);
                if (varData != null)
                {
                    item.InputValue = varData.InputValue;

                    if (!string.IsNullOrEmpty(item.CalculatedValue))
                    {
                                   mop = mop.Replace(item.UserKeyValue, item.CalculatedValue);
                     }
                }
            }
            // Add color to the variables
            mop = mop.Replace("[{", "<span style=\"color:Red;font-weight:bold;\">[{").Replace("}]", "}]</span>");
            mop = mop.ColorizeCommentsWith(new string[] { "//", "!" }, "color:Green; font-weight:bold").ColorizeCommentsWith(new string[] { "/*" }, "color:#3399FF");
            mop = "<h2><div id=\"mopTitle\">MOP TITLE</div></h2>" + mop;
            return MvcHtmlString.Create(mop.ToHtml());
        }

        [HttpPost]
        [ValidateInput(false)]
        public String SaveMOP(String title, String mop)
        {
            var ret = "Save Successfully ";
            var rootPath = AppDomain.CurrentDomain.BaseDirectory + "App_Data\\DataStore\\MOPs\\";
            var fileName = title.ToSlug() + String.Format("_{0:yyyyMMddmmss}_mop",DateTime.Now) + ".htm";
            if ((String.IsNullOrEmpty(title)))
                title = "MOP";
                try
                {
                    System.IO.File.WriteAllText(rootPath + fileName, mop);
                }
                catch
                {
                    ret = "Unable to save! Error saving file!";
                }


            return ret;
        }


    }


}
